﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpNewFeatures
{
    internal class CsharpDemo3 : CsharpDemoBase
    {

        public override void Run()
        {

            // 1. 匿名类
            var tom = new { ID = 0, Name = "Tom" };
            Console.WriteLine($"tom.ID={tom.ID}, tom.Name={tom.Name}");

            // 2. 三种代理： Func, Action 和 Predicate

            // 如何取最后一段路径 MTD2032
            var s = @"C:\data\2022\04\MTD2032";
            Console.WriteLine("Last Part: s.Split('\\')[^1] = " + s.Split('\\')[^1]);
            // Console.WriteLine("Last Part: s.Split('\\')[^0] = " + s.Split('\\')[^0]);
            Console.WriteLine("Last Part: s.Split('\\')[^3..^0] = " + s.Split('\\')[^3..^0]);

            Console.WriteLine($"Range ^3..^0: {^3..^0}");
            Console.WriteLine($"Range ^3..0: {^3..0}");
            Console.WriteLine((^0).GetType().FullName);

            // System.Index 测试，本质含意的理解：
            // 给定数组 int[] values, 则 ^x 的本质含意是取 第 values.Length - x 个元素，
            // 如 ^0 取第 values.Length - 0 个元素，所以 values[^0] 肯定会报错
            var id1 = new Index(4, false); 
            var id2 = ^2;
            Index id3 = 1;
            Console.WriteLine($"new Index(4, false): {id1}, ^2: {id2}, 1: {id3}");


            int[] values = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
            // System.Range
            var range1 = new Range(id1, id2);
            Console.WriteLine(range1.GetOffsetAndLength(values.Length));
            Console.WriteLine(String.Join(",", values[range1]));

            Console.WriteLine($"[1, ^1)|5: {(1..^1).GetOffsetAndLength(5)}");
            // Console.WriteLine($"[8, ^5)|5: {(4..^3).GetOffsetAndLength(5)}"); 

            //  本地静态函数
            Console.WriteLine($"Add(3,4) = {Add(3, 4)}");

            static int Add(int left, int right) => left + right;


            printTitle("空值检测符?");
            int[] arr1 = { 1,2,3,4,5,6 };
            int[] arr2 = null;
            Console.WriteLine($"int[] arr1 = {{1,2,3,4,5,6 }};");
            Console.WriteLine($"int[] arr2 = null;");
            Console.WriteLine($"arr1?[0]: {arr1?[0]}");
            Console.WriteLine($"arr1?[10] shall arise System.IndexOutOfRangeException exception.");
            Console.WriteLine($"arr2?[0]: {arr2?[0]}");
            Console.WriteLine($"arr2?[0] ?? -1: {arr2?[0] ?? -1}");
        }

    }
}
